URL_ACTIVE = "ftp://xmlsoft.org/libxml2/libxml2-%(VERSION)s.tar.gz"
URL_OLD = "http://xmlsoft.org/sources/old/libxml2-%(VERSION)s.tar.gz"

def processurl(machine, dirs, version):
  if version in HISTORIC_VERSIONS:
    return URL_OLD % {"VERSION": version }
  else:
    return URL_ACTIVE % {"VERSION" : version }

URL = processurl

HISTORIC_VERSIONS = ["2.4.14"]
VERSIONS = HISTORIC_VERSIONS + ["2.7.7", "2.7.8"]
DEFAULT_VERSION = "2.7.8"
DEPENDS = ["zlib"]

# Windows: NMakeBuilder
# Linux: Todo zlib dep from system

import os, shutil, subprocess
from source.builders import NMakeBuilder, ConfigureMakeBuilder
from source.machine import Collector
import ext.patch

def getlibxml2dir(machine, directories):
  return directories.enviro+"/"+"libxml2-"+dotversion

class LibXML2Builder( NMakeBuilder ):
  """ subclass of CMakeBuilder with custom configure step """

  def patch(self):
    os.chdir( self.extractedpath )
    #only patch version 2.7.8
    if (self.version in ('2.7.8')):
      patchstr = patch778 = """
diff --git a/win32/Makefile.msvc b/win32/Makefile.msvc
index bd0eb9d..ff4c960 100644
--- win32/Makefile.msvc
+++ win32/Makefile.msvc
@@ -71,9 +71,9 @@ LIBS = $(LIBS) wsock32.lib ws2_32.lib
 !if "$(WITH_ICONV)" == "1"
 LIBS = $(LIBS) iconv.lib
 !endif 
-+!if "$(WITH_ICU)" == "1"
-+LIBS = $(LIBS) icu.lib
-+!endif
+!if "$(WITH_ICU)" == "1"
+LIBS = $(LIBS) icu.lib
+!endif
 !if "$(WITH_ZLIB)" == "1"
 LIBS = $(LIBS) zdll.lib
 !endif
"""
      p = ext.patch.fromstring(patchstr)
      print "Patch applied: ", p.apply()

      
  def create3rdParty( self ):
    """get the directories of the 3rd party libs from the other modules into one dir"""
    
    #import build_zlib #build_libiconv #no iconv support currently
    #libiconv_dir = build_libiconv.getlibiconvdir(machine, dirs)
    #zlib_dir = build_zlib.getzlibdir(machine, dirs)
    
    thirdPartyDir = self.directories.build+"\\libxml2ext"
    
    Collector([ self.dependencies["zlib"].targetpath() ], thirdPartyDir, removeandcreatedir = True)
    #if os.path.exists(thirdPartyDir+"\\lib\\win_iconv.lib"):
    #  shutil.copy( thirdPartyDir+"\\lib\\win_iconv.lib", thirdPartyDir+"\\lib\\iconv.lib" )
    shutil.copy( thirdPartyDir+"\\lib\\zlib.lib", thirdPartyDir+"\\lib\\zdll.lib" )
   
 
  def configure( self ):
    self.create3rdParty()
    os.chdir( self.extractedpath+"\\win32" )

    command = "cscript configure.js compiler=msvc "
    
    installpath = self.installpath()
    if self.debug:
      installpath += "\\debug"
      command += " debug=yes"      
    else:
      command += " debug=no"
      
    command += " prefix="+installpath
    command += " sodir="+installpath+"\\bin " 
    command += " include="+self.directories.build+"\\libxml2ext\\include"
    command += " lib="+self.directories.build+"\\libxml2ext\\lib"
    command += " zlib=yes"
    command += " iconv=no"
    command += " vcmanifest=yes"
      
    print( "executing: "+command )
    self.execute(command)
    
  def makefile(self):
    return "Makefile.msvc"

  def make(self ):
    os.chdir( self.extractedpath+"\\win32" )
    # Extra clean. Building debug and release may merge the manifest 
    # files if not cleaned first.
    self.execute( "nmake /f " + self.makefile() + " clean" ) 
    self.execute( "nmake /f " + self.makefile() )

  def install(self):
    self.execute( "nmake install " )
    #import build_libiconv #no iconv support currently
    #shutil.copy2( build_libiconv.getlibiconvdir(self.machine, self.directories)+"\\include\\iconv.h", self.installpath+"\\include" )

 
class LinuxLibXML2Builder( ConfigureMakeBuilder ):
  def configureparameters( self ):
    params = []
    if self.version in HISTORIC_VERSIONS:
      params.append( "--without-http" ) # Compile error on a modern system.

    return params
 

def decider( machine, dirs ):
  """ build png for the machine in dirs """
  return (LinuxLibXML2Builder, LibXML2Builder)[machine.isWindows()]


BUILDER = decider
